﻿//#define PY_SSIZE_T_CLEAN
//#define WXUSINGDLL
//#define __WXMSW__

#include <Python.h>
#include <Windows.h>
//size_t s1 =1;
//#define ZZ sizeof( size_t )
//#if ZZ == sizeof( unsigned _int64 )
//#pragma message ( "sizeof: "sizeof( size_t ))
//#endif
//#include <cmath>
//#include <wx/wx.h>

//extern "C"
//extern "C++"
extern "C" {
    size_t s2 =1;
    const double e = 2.7182818284590452353602874713527;

    double sinh_impl(double x) {
        return (1 - pow(e, (-2 * x))) / (2 * pow(e, -x));
    }

    double cosh_impl(double x) {
        return (1 + pow(e, (-2 * x))) / (2 * pow(e, -x));
    }

    /*
    fast_tanh(3)
    */
    PyObject* tanh_impl(PyObject*, PyObject* o) {
        double x = PyFloat_AsDouble(o);
        double tanh_x = sinh_impl(x) / cosh_impl(x);
        return PyFloat_FromDouble(tanh_x);
    }

    static int postMessage(Py_UNICODE* path, int len) {
        //wxFileName fileName(path);
        WIN32_FIND_DATAW data;
        HANDLE h = FindFirstFileW(path, &data);
        int retVal = 0;
        if (h != INVALID_HANDLE_VALUE) {
            HWND hwnd = ::FindWindow(nullptr, ("Transfer"));
            if (hwnd) {
                auto* buff = new wchar_t[len + 2];
                wcscpy_s(buff, len * 2 + 2, path);
                COPYDATASTRUCT cds;
                cds.dwData = 1;
                cds.lpData = buff;
                cds.cbData = len * 2;
                ::SendMessage(hwnd, WM_COPYDATA, 0, (LPARAM)&cds);
            }
            else {
                MessageBox(nullptr, "目标窗口未发现", "Info", MB_OK | MB_ICONERROR);
                retVal = -1;
            }
            CloseHandle(h);
        }
        else {
            //MessageBox(0, "文件不存在", "文件不存在", MB_OK | MB_ICONERROR );
            MessageBoxW(nullptr, path, L"文件不存在", MB_OK | MB_ICONERROR);
            return -1;
        }
        return retVal;
    }

    //PyObject* self, PyObject* args
    //
    PyObject* info_impl(PyObject*, PyObject* args) {
        int ok;
        //char* msg;
        //https://blog.csdn.net/haojiangcong/article/details/115069329
        //ok = PyArg_ParseTuple(args, "s", &s); /* A string */
        //if (!PyArg_ParseTuple(args, "s", &msg )) {            // Argument is an Numpy array object
        //	return nullptr;
        //}
        //double x = PyFloat_AsDouble(o);
        //double tanh_x = sinh_impl(x) / cosh_impl(x);
        //MessageBox(0, "123", "456", MB_OK);
        //wchar_t wname[100];

        //setlocale(LC_ALL, "chinese");
        //int nLen = MultiByteToWideChar(CP_ACP, 0, msg, -1, NULL, NULL);
        //MultiByteToWideChar(CP_ACP, 0,
        //	msg, -1, wname, nLen);
        //
        //wchar_t* _msg = (wchar_t*)msg;
        //MessageBox(0, msg, "123", MB_OK);
        //MessageBoxW(0, _msg, _msg, MB_OK);
        //"u#" (Unicode object)[Py_UNICODE*, int]
        Py_UNICODE* msg;
        int len;
        if (!PyArg_ParseTuple(args, "u#", &msg, &len)) {            // Argument is an Numpy array object
            return nullptr;
        }
        //MessageBoxW(0, msg, msg, MB_OK);
        int retVal = postMessage(msg, len);
        return PyFloat_FromDouble(retVal);
    }

    //METH_VARARGS 常量表示参数以元组形式传入
    ////函数还有参数类型：
    //METH_VARARGS:存在*args
    //METH_KEYWORDS:存在**kwargs
    //METH_NOARGS:不接受参数
    //METH_O:接收一个参数
    static PyMethodDef superfastcode_methods[] = {
            // The first property is the name exposed to Python, fast_tanh, the second is the C++
            // function name that contains the implementation.
            { "fast_tanh", (PyCFunction)tanh_impl, METH_O, nullptr },
            { "info", (PyCFunction)info_impl, METH_VARARGS, nullptr },
            // Terminate the array with an object containing nulls.
            { nullptr, nullptr, 0, nullptr }
    };

    static PyModuleDef post_file_module = {
            PyModuleDef_HEAD_INIT,
            "postFile",                        // Module name to use with Python import statements
            "Post File",  // Module description
            0,
            superfastcode_methods                   // Structure that defines the methods of the module
    };

    PyMODINIT_FUNC PyInit_postFile() {
        return PyModule_Create(&post_file_module);
    }
}